
//-----------------MATRICES---------------
float4x4 xWorld: World;				
float4x4 xView: View;
float4x4 xProjection: Projection;

//-----------------TEXTURES---------------

Texture xTextureSnow;
sampler ColoredSnowTextureSampler = sampler_state { texture = <xTextureSnow> ;
    magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};
	
Texture xTextureGround;
sampler ColoredGroundTextureSampler = sampler_state { texture = <xTextureGround> ;
    magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};

Texture xTextureGrass;
sampler ColoredGrassTextureSampler = sampler_state { texture = <xTextureGrass> ;
    magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};
	
Texture xTextureRock;
sampler ColoredRockTextureSampler = sampler_state { texture = <xTextureRock> ;
    magfilter = LINEAR; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};
//-----------------CONSTANTS----------------

float4 xLightPosition;		// pozycja swiatla
float4 xObserverPosition;	// pozycja obserwatora
float xMaxHeight;

//------------------- STRUCTS -------------------   


struct BufferToVertex
{
float3 Pos : POSITION;
float4 Normal : NORMAL;
float4 UV : TEXCOORD0;
};


struct VertexToPixel
{
float4 Pos:POSITION;
float4 UV:TEXCOORD0;
float4 Pos3D:TEXCOORD1;
float4 Normal:TEXCOORD2;
float4 Color:COLOR;
float4 TexWeights: TEXCOORD3;
};


struct PixelToFrame
{
float4 Color:COLOR;
};





//---------------------- SHADERS ------------------------

VertexToPixel terrainVS(BufferToVertex IN)
{
  	VertexToPixel OUT = (VertexToPixel)0; 
  	float4x4 WorldViewProj;
  
  	WorldViewProj=mul(xWorld,xView);
  	WorldViewProj=mul(WorldViewProj,xProjection);
  
  	OUT.Pos=mul(float4(IN.Pos.xyz,1.0), WorldViewProj);


  	OUT.Pos3D=mul(float4(IN.Pos.xyz,1.0),xWorld);  
  	OUT.Normal=IN.Normal;  
  	OUT.UV=float4(IN.UV.xy, 1.0,1.0);
	
	float t=(IN.Pos.y)/(xMaxHeight);
	if (t<(1.0/7.0)) OUT.TexWeights=float4(1.0,0.0,0.0,0.0);
	if ((t>=(1.0/7.0)) && ((t<2.0/7.0))) OUT.TexWeights=float4(2.0-7.0*t,7.0*t-1,0.0,0.0);
	if ((t>=(2.0/7.0)) && ((t<3.0/7.0))) OUT.TexWeights=float4(0.0,1.0,0.0,0.0);
	if ((t>=(3.0/7.0)) && ((t<4.0/7.0))) OUT.TexWeights=float4(0.0,4.0-7.0*t,7.0*t-3,0.0);
	if ((t>=(4.0/7.0)) && ((t<5.0/7.0))) OUT.TexWeights=float4(0.0,0.0,1.0,0.0);
	if ((t>=(5.0/7.0)) && ((t<6.0/7.0))) OUT.TexWeights=float4(0.0,0.0,6.0-7.0*t,7.0*t-5.0);
	if (t>=(6.0/7.0)) OUT.TexWeights=float4(0.0,0.0,0.0,1.0);
  
  	return OUT;
}


PixelToFrame terrainPS(VertexToPixel IN) 
{
	PixelToFrame OUT = (PixelToFrame)0;
	float4 LightVector = normalize(xLightPosition-IN.Pos3D);
	float LightIntensity = dot(LightVector, IN.Normal);

	OUT.Color = saturate((tex2D(ColoredGroundTextureSampler, IN.UV)*IN.TexWeights.x+
       		          tex2D(ColoredGrassTextureSampler, IN.UV)*IN.TexWeights.y+
                      tex2D(ColoredRockTextureSampler, IN.UV)*IN.TexWeights.z+
                      tex2D(ColoredSnowTextureSampler, IN.UV)*IN.TexWeights.w)*LightIntensity + float4(0.2, 0.2, 0.1, 1));
	//OUT.Color = IN.Normal; 
	return OUT;
}

VertexToPixel normalsVS(BufferToVertex IN)
{
  	VertexToPixel OUT = (VertexToPixel)0; 
  	float4x4 WorldViewProj;
  
  	WorldViewProj=mul(xWorld,xView);
  	WorldViewProj=mul(WorldViewProj,xProjection);
  
  	OUT.Pos=mul(float4(IN.Pos.xyz,1.0), WorldViewProj);

  	OUT.Normal=IN.Normal;  
  
  	return OUT;
}


PixelToFrame normalsPS(VertexToPixel IN) 
{
	PixelToFrame OUT = (PixelToFrame)0;

	OUT.Color = IN.Normal; 
	return OUT;
}



// ------------ TECHNIQUES --------------

technique terrain{
	pass p0 {
		VertexShader = compile vs_3_0 terrainVS();
		PixelShader = compile ps_3_0 terrainPS();
	}
}

technique normals{
	pass p0 {
		VertexShader = compile vs_3_0 normalsVS();
		PixelShader = compile ps_3_0 normalsPS();
	}
}

